/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trabalho.pkg3;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Matheus Cintra
 */
public class InfoArq {
    private String NomeArq;
    private long Size;
    private String Hosts;
    private File Arq ; //Arquivo com as informaçoes do arquivo
    private File dir = new File("INFO");
    
    public InfoArq(String nome){
        String filepath = nome + ".obj";
        Arq = new File(dir, filepath);
        this.setNomeArq(nome);
    }
    
    public boolean load(){
        if( dir.mkdir() || dir.exists()){
            try {
                try (ObjectInputStream reader = new ObjectInputStream(new FileInputStream(Arq))) {
                    this.NomeArq = (String) reader.readObject();
                    this.Size = (long) reader.readObject();
                    this.Hosts = (String) reader.readObject();                     
                }
            } catch (ClassNotFoundException | IOException ex) {
                Logger.getLogger(InfoArq.class.getName()).log(Level.SEVERE, null, ex);
                return false; //Arquivo não existe ou erro ao ler o arquivo
            }
        }
        
        return true;
    }
    
    public void grava() throws IOException{
        if( dir.mkdir() || dir.exists()){
            try (ObjectOutputStream saida = new ObjectOutputStream(new FileOutputStream(Arq))) {
                saida.writeObject(this.getNomeArq());
                saida.writeObject(this.getSize());
                saida.writeObject(this.getHosts());
            }
        }
    }
    
    public boolean verificar(String host){
        String []nome;
        nome = Hosts.split(";");
        for(int i = 0; i < nome.length; i++){
            if(nome[i].equals(host)) {
                return true;
            }
        }
        return false;
    }
    
    public void atualizar_host(String nome){
        Hosts = Hosts + ";" + nome;
    }

    public void setNomeArq(String NomeArq) {
        this.NomeArq = NomeArq;
    }

    public void setSize(long Size) {
        this.Size = Size;
    }

    public void setHosts(String Hosts) {
        this.Hosts = Hosts;
    }   

    public String getNomeArq() {
        return NomeArq;
    }

    public long getSize() {
        return Size;
    }

    public String getHosts() {
        return Hosts;
    }
    
}
